/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package origendatos.dyd.model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import origendatos.dyd.model.exceptions.NonexistentEntityException;
import origendatos.dyd.model.exceptions.PreexistingEntityException;

/**
 *
 * @author erodrig
 */
public class ArmadurasJpaController {

    public ArmadurasJpaController() {
        emf = Persistence.createEntityManagerFactory("OrigenDatosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Armaduras armaduras) throws PreexistingEntityException, Exception {
        if (armaduras.getPjList() == null) {
            armaduras.setPjList(new ArrayList<Pj>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Pj> attachedPjList = new ArrayList<Pj>();
            for (Pj pjListPjToAttach : armaduras.getPjList()) {
                pjListPjToAttach = em.getReference(pjListPjToAttach.getClass(), pjListPjToAttach.getId());
                attachedPjList.add(pjListPjToAttach);
            }
            armaduras.setPjList(attachedPjList);
            em.persist(armaduras);
            for (Pj pjListPj : armaduras.getPjList()) {
                Armaduras oldArmadurasOfPjListPj = pjListPj.getArmaduras();
                pjListPj.setArmaduras(armaduras);
                pjListPj = em.merge(pjListPj);
                if (oldArmadurasOfPjListPj != null) {
                    oldArmadurasOfPjListPj.getPjList().remove(pjListPj);
                    oldArmadurasOfPjListPj = em.merge(oldArmadurasOfPjListPj);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findArmaduras(armaduras.getNombre()) != null) {
                throw new PreexistingEntityException("Armaduras " + armaduras + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Armaduras armaduras) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Armaduras persistentArmaduras = em.find(Armaduras.class, armaduras.getNombre());
            List<Pj> pjListOld = persistentArmaduras.getPjList();
            List<Pj> pjListNew = armaduras.getPjList();
            List<Pj> attachedPjListNew = new ArrayList<Pj>();
            for (Pj pjListNewPjToAttach : pjListNew) {
                pjListNewPjToAttach = em.getReference(pjListNewPjToAttach.getClass(), pjListNewPjToAttach.getId());
                attachedPjListNew.add(pjListNewPjToAttach);
            }
            pjListNew = attachedPjListNew;
            armaduras.setPjList(pjListNew);
            armaduras = em.merge(armaduras);
            for (Pj pjListOldPj : pjListOld) {
                if (!pjListNew.contains(pjListOldPj)) {
                    pjListOldPj.setArmaduras(null);
                    pjListOldPj = em.merge(pjListOldPj);
                }
            }
            for (Pj pjListNewPj : pjListNew) {
                if (!pjListOld.contains(pjListNewPj)) {
                    Armaduras oldArmadurasOfPjListNewPj = pjListNewPj.getArmaduras();
                    pjListNewPj.setArmaduras(armaduras);
                    pjListNewPj = em.merge(pjListNewPj);
                    if (oldArmadurasOfPjListNewPj != null && !oldArmadurasOfPjListNewPj.equals(armaduras)) {
                        oldArmadurasOfPjListNewPj.getPjList().remove(pjListNewPj);
                        oldArmadurasOfPjListNewPj = em.merge(oldArmadurasOfPjListNewPj);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = armaduras.getNombre();
                if (findArmaduras(id) == null) {
                    throw new NonexistentEntityException("The armaduras with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Armaduras armaduras;
            try {
                armaduras = em.getReference(Armaduras.class, id);
                armaduras.getNombre();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The armaduras with id " + id + " no longer exists.", enfe);
            }
            List<Pj> pjList = armaduras.getPjList();
            for (Pj pjListPj : pjList) {
                pjListPj.setArmaduras(null);
                pjListPj = em.merge(pjListPj);
            }
            em.remove(armaduras);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Armaduras> findArmadurasEntities() {
        return findArmadurasEntities(true, -1, -1);
    }

    public List<Armaduras> findArmadurasEntities(int maxResults, int firstResult) {
        return findArmadurasEntities(false, maxResults, firstResult);
    }

    private List<Armaduras> findArmadurasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Armaduras.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Armaduras findArmaduras(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Armaduras.class, id);
        } finally {
            em.close();
        }
    }

    public int getArmadurasCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Armaduras> rt = cq.from(Armaduras.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
